<template>
  <div class="view-container">
    <el-form :model="queryParam" label-width="auto">
      <el-row :gutter="10">
        <el-col :span="6">
          <el-form-item label="任务名称" prop="jobName">
            <el-input v-model="queryParam.jobName" placeholder="任务名称"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="任务执行器" prop="jobHandler">
            <el-input v-model="queryParam.jobHandler" placeholder="任务执行器"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item>
            <el-button type="primary" @click="handleQuery">搜索</el-button>
            <el-button @click="resetParams">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <nebula-table :columns="columns" :data="tableData" @change="changePage"
                  use-tool-bar border show-overflow-tooltip :total="rowsTotal" header-row-class-name="header-row-class">
      <template #head-toolbar>
        <el-button type="primary" @click="handleCreate" size="small">新增</el-button>
      </template>
      <template #jobHandler="scope">
        <router-link :to=genDetailUrl(scope.row) class="table-link">
          {{ scope.row.jobHandler }}
        </router-link>
      </template>
      <template #isOpen="scope">
        <el-switch v-model="scope.row.isOpen" :active-value="1" :inactive-value="0" active-text="启用"
                   inactive-text="停用" @change="switchChange(scope)" inline-prompt></el-switch>
      </template>
      <template #operation="scope">
        <el-button type="primary" size="small" @click="handleEdit(scope.row)">编辑</el-button>
        <el-button type="danger" size="small" @click="handleDelete(scope.row)">删除</el-button>
      </template>
    </nebula-table>
    <el-dialog v-model="isShow" title="新增任务">
      <div class="createForm view-container">
        <el-form :model="createParam" label-width="auto">
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="任务名称" prop="jobName">
                <el-input v-model="createParam.jobName"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="执行器" prop="jobHandler">
                <el-input v-model="createParam.jobHandler"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="CRON" prop="cron">
                <el-input v-model="createParam.cron"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="任务状态" prop="isOpen">
                <el-select v-model="createParam.isOpen" :show-search="true" class="width-control"
                           allow-clear>
                  <el-option :value="1" label="启用"></el-option>
                  <el-option :value="0" label="停用"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="任务参数" prop="jobParams">
                <el-input placeholder="请输入任务参数" v-model="createParam.jobParams" type="textarea">
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="任务描述" prop="jobDesc">
                <el-input placeholder="请输入任务描述" v-model="createParam.jobDesc" type="textarea">
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <template #footer>
        <el-button @click="createCancel">取消</el-button>
        <el-button type="primary" @click="createSubmit">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>
<script setup>
import {createOrEditTask, getTaskTableData, modifyTaskOpen} from "@/api/sysTask.js";
import NebulaTable from "@/components/NebulaTable.vue";
import {ElMessageBox, ElNotification} from "element-plus";

defineOptions({
  name: 'sysTask'
})

const queryParam = ref({
  jobName: null,
  jobHandler: null,
  pageNum: 1,
  pageSize: 10
})

const columns = [
  {title: "任务名称", key: "jobName", align: 'center'},
  {title: "执行器", key: "jobHandler", align: 'center', customSlot: 'jobHandler'},
  {title: "CRON", key: "cron", align: 'center'},
  {title: "运行参数", key: "jobParams", align: 'center'},
  {title: "运行状态", key: "isOpen", align: 'center', customSlot: 'isOpen'},
  {title: "创建时间", key: "createTime", align: 'center'},
  {title: "任务描述", key: "jobDesc", ellipsisTooltip: true, ellipsisTooltipTheme: 'dark'},
  {title: "操作", align: 'center', customSlot: 'operation'}
]

const rowsTotal = ref(0)
const tableData = ref([])

const page = reactive({
  current: queryParam.value.pageNum,
  limit: queryParam.value.pageSize,
  total: rowsTotal,
  layout: ["prev", "page", "next", "limits", "count"],
});

const createParam = ref({
  jobName: null,
  jobHandler: null,
  cron: null,
  jobParams: null,
  isOpen: 1,
  jobDesc: null
})
const isShow = ref(false)

const createCancel = () => {
  isShow.value = false
  for (let key in createParam.value) {
    createParam.value[key] = null;
  }
}

const createSubmit = () => {
  createOrEditTask(createParam.value).then(res => {
    if (res.code === 200) {
      isShow.value = false
      handleQuery()
      ElNotification({
        title: "操作成功",
        type: "success"
      })
    } else {
      ElNotification({
        title: "操作失败",
        type: "error",
        message: res.msg
      })
    }
  })
}

const changePage = (currentPage,pageSize) => {
  queryParam.value.pageNum = currentPage
  queryParam.value.pageSize = pageSize
  handleQuery()
}

const genDetailUrl = (row) => {
  return "/monitor/task/detail/" + row.id;
}

const handleQuery = () => {
  getTaskTableData(queryParam.value).then(res => {
    tableData.value = res.data.rows
    rowsTotal.value = res.data.total
  })
}

const resetParams = () => {
  for (let key in queryParam.value) {
    if (key !== "pageNum" && key !== "pageSize") {
      queryParam.value[key] = null;
    }
  }
}

const switchChange = (scope) => {
  const row = scope.row
  const index = scope.index
  if (index === -1) return
  let param = {
    id: row.id
  }
  modifyTaskOpen(param).then(res => {
    if (res.code === 200) {
      handleQuery()
      ElNotification({
        title: "操作成功",
        type: "success"
      })
    }
  })
}

const handleCreate = () => {
  isShow.value = true;
}

const handleEdit = (row) => {
  createParam.value = {...row}
  isShow.value = true
}

const handleDelete = (row) => {
  const confirmText = `确定要删除任务【${row.jobName}】吗?`
  ElMessageBox.confirm(
      confirmText,
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }
  ).then(() => {
    const changeParam = {
      id: row.id,
      isDeleted: 1
    }
    createOrEditTask(changeParam).then(res => {
      if (res.code === 200) {
        handleQuery()
        ElNotification({
          title: "操作成功",
          type: "success"
        })
      } else {
        ElNotification({
          title: "操作失败",
          type: "error",
          message: res.msg
        })
      }
    })
  })
}

onMounted(() => {
  handleQuery()
})
</script>
